package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.PayRoll;
import cn.wolfcode.crm.query.PayRollQueryObject;
import cn.wolfcode.crm.service.IPayRollService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("payRoll")
public class PayRollController {
    @Autowired
    private IPayRollService service;

    @RequiresPermissions(value = {"payRoll:view", "工资视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "payRoll/payRoll";
    }

    @RequiresPermissions(value = {"payRoll:query", "工资列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(@ModelAttribute("qo") PayRollQueryObject qo) {
        System.out.println(qo);
        return service.query(qo);
    }

    @RequiresPermissions(value = {"payRoll:selectAll", "工资全选"}, logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<PayRoll> selectAll() {
        return service.selectAll();
    }

    @RequiresPermissions(value = {"payRoll:saveOrUpdate", "工资编辑"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(PayRoll payRoll) {
        try {
            service.updateByPrimaryKey(payRoll);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("提交失败");
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"payRoll:remove", "工资删除"}, logical = Logical.OR)
    @RequestMapping("remove")
    @ResponseBody
    public JsonResult remove(Long id) {
        try {
            if (id != null) {
                service.deleteByPrimaryKey(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("提交失败");
        }
        return new JsonResult();
    }

    @RequiresPermissions(value = {"payRoll:remove", "工资核算"}, logical = Logical.OR)
    @RequestMapping("onState")
    @ResponseBody
    public JsonResult onState(Long id) {
        try {
            service.onState(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, PayRollQueryObject qo) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=payRoll.xls");

        Workbook book = service.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }


    @RequestMapping("importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file, HttpServletResponse response) throws Exception {
        try {
            service.importXls(file, response);
            return new JsonResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult().mark(e.getMessage());
        }
    }

    /**
     * 获取文件上传模板
     *
     * @param response
     * @throws Exception
     */
    @RequestMapping("getImportXlsMode")
    public void importXlsMode(HttpServletResponse response) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=payRoll.xls");

        Workbook book = service.getImportXlsMode();

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }
}